package com.auto.learning.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.PlayListActivity;
import com.auto.learning.bean.BookBean;
import com.auto.learning.bean.DownLoadView;
import com.auto.learning.event.EventBus;
import com.auto.learning.event.Handle;
import com.auto.learning.event.ProgressEvent;
import com.auto.learning.service.DownLoadManager;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.TemporaryVariable;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;

public class PlayListAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<BookBean> list;
	private Handle handle;
	
	private ProgressEvent progress;
	
	
	@SuppressLint("UseSparseArrays")
	private Map<Integer, DownLoadView> map = new HashMap<Integer, DownLoadView>();
	
	
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				/*ProgressEvent event = (ProgressEvent) msg.obj;
				DownLoadView downLoadView = map.get(progress.getBookId());
				if (downLoadView != null ) {
					if (downLoadView.getTextView().getTag().toString().equals(""+progress.getBookId()) && progress.getCurrentRatio() < 100) {
//						Log.d("ProgressEvent", event.getCurrentRatio() + "--" + event.getBookId());
						downLoadView.getTextView().setVisibility(View.VISIBLE);
						downLoadView.getTextView().setText(progress.getCurrentRatio()+"%");
						downLoadView.getImageView().setVisibility(View.GONE);
					}else if(event.getCurrentRatio() == 100){
						downLoadView.getTextView().setVisibility(View.GONE);
						downLoadView.getImageView().setVisibility(View.VISIBLE);
						notifyDataSetChanged();
					}
				}*/
				break;
			}
		};
	};

	public PlayListAdapter(Context context, ArrayList<BookBean> list, Handle handle,ProgressEvent progress) {
		this.context = context;
		this.list = list;
		this.handle = handle;
		this.progress = progress;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return list.get(position).getBookId();
	}
	
	
	public void refreshProgress(ProgressEvent event){
		/*DownLoadView downLoadView = map.get(event.getBookId());
		if (downLoadView != null ) {
			if (downLoadView.getTextView().getTag().toString().equals(""+event.getBookId())) {
				downLoadView.getTextView().setVisibility(View.VISIBLE);
				downLoadView.getTextView().setText(event.getCurrentRatio()+"%");
				downLoadView.getImageView().setVisibility(View.GONE);
			}else if(event.getCurrentRatio() == 100){
				downLoadView.getTextView().setVisibility(View.GONE);
				downLoadView.getImageView().setVisibility(View.VISIBLE);
//				notifyDataSetChanged();
			}
		}*/
		this.progress = event;
		notifyDataSetChanged();
	}

	ViewHolder holder;
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final BookBean bookBean = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.play_list_adapter, null);
			holder = new ViewHolder();
			holder.rel_left = (RelativeLayout) convertView.findViewById(R.id.rel_left);
			holder.image_play_status = (ImageView) convertView.findViewById(R.id.image_play_status);
			holder.image_download = (ImageView) convertView.findViewById(R.id.image_download);
			holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.linear_right = (RelativeLayout) convertView.findViewById(R.id.linear_right);
			holder.tv_progress = (TextView) convertView.findViewById(R.id.tv_progress);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.tv_progress.setTag(bookBean.getBookId());
		holder.tv_title.setText(bookBean.getBookName() +"");
		int ids[] = MusicPlayManager.getInstance(context).isPlayingBookId();
		if (ids[0] == bookBean.getBookId()) {
			holder.tv_title.setTextColor(context.getResources().getColor(R.color.main_green_color));
			if (ids[2] == 0) {
				holder.image_play_status.setImageResource(R.drawable.pause_small);
			}else {
				holder.image_play_status.setImageResource(R.drawable.play_yuedu2);
			}
		}else {
			holder.tv_title.setTextColor(context.getResources().getColor(R.color.main_text_color));
			holder.image_play_status.setImageResource(R.drawable.play_small);
		}
		if (progress != null) {
			if (progress.getBookId() == bookBean.getBookId() && progress.getCurrentRatio() < 100 && progress.getCurrentRatio() > 0) {
				holder.tv_progress.setVisibility(View.VISIBLE);
				holder.image_download.setVisibility(View.GONE);
				holder.tv_progress.setText(progress.getCurrentRatio()+"%");
			} else {
				if (TemporaryVariable.temp_download_all_list.contains(bookBean.getBookId()) && !TemporaryVariable.downLoadedList.contains(bookBean.getBookId())) {
					holder.tv_progress.setText("0%");
					holder.tv_progress.setVisibility(View.VISIBLE);
					holder.image_download.setVisibility(View.GONE);
				}else{
					holder.tv_progress.setVisibility(View.GONE);
					holder.image_download.setVisibility(View.VISIBLE);
				}
			}
		} else {
			holder.tv_progress.setVisibility(View.GONE);
			holder.image_download.setVisibility(View.VISIBLE);
		}
		
		if (TemporaryVariable.downLoadedList.contains(bookBean.getBookId())) {
			holder.image_download.setImageResource(R.drawable.xiazaiok);
		}else {
			holder.image_download.setImageResource(R.drawable.playlist_download);
		}
		
		holder.linear_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				handle.obtainMessage(2,bookBean).sendToTarget();
			}
		});
		holder.rel_left.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				handle.obtainMessage(3, position, 0, bookBean.getBookId()).sendToTarget();
			}
		});
		map.put(bookBean.getBookId(), new DownLoadView(bookBean.getBookId(),holder.tv_progress,holder.image_download));
		return convertView;
	}

	class ViewHolder {
		RelativeLayout rel_left,linear_right;
		ImageView image_play_status, image_download;
		TextView tv_title,tv_progress;
	}
}
